package GUI;
/**
 * Teacher allocation to class  screen
 */
import javax.swing.JPanel;
import java.awt.SystemColor;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;

import Common.CourseOfStudent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

public class TeacherAllocationToClass extends JPanel {
		private JLabel lblExceptionalRequest = null;
		private JLabel lblSelectSubstituteTeacher = null;
		private JComboBox cBRplcTeacher = null;
		private JButton btnRequestAuthorization = null;
		
		public Login myLoginFrame = null;
		public UserAdmin user=null;
		public AdminMainMenu amn=null;	
		
		String selRplcTeacher = null;
	
	private JComboBox getcBRplcTeacher(AdminMainMenu amn)
	{
		
		
		int i,j;
		CourseOfClass temp=new CourseOfClass()  ; 
		
		for(j=0;j<amn.user.DataForEx.CoursesOfClasses.size();j++)
		{
			if(amn.user.DataForEx.getSelCourse().endsWith(amn.user.DataForEx.CoursesOfClasses.get(j).Cid))
					{
				temp = amn.user.DataForEx.CoursesOfClasses.get(j);
					}
		}
		Vector<String> dataTeachers = new Vector<String>();
		for(i=0;i<amn.user.DataForEx.Teachers.size();i++)
		{
			
			System.out.println("Test 1 "+amn.user.DataForEx.Teachers.get(i).StudUnit);
			System.out.println("Test 2 "+amn.user.DataForEx.getSelStudyUnit());
			System.out.println("Test 3 "+amn.user.DataForEx.Teachers.get(i).tid);
			System.out.println("Test 4 "+amn.user.DataForEx.getSelSubsTeacher());
			System.out.println("Test 4 "+temp.StudHours);
			System.out.println("Test 4 "+amn.user.DataForEx.Teachers.get(i).FreeHours);
			
		if(amn.user.DataForEx.Teachers.get(i).StudUnit.equals(amn.user.DataForEx.getSelStudyUnit())&&
				(!amn.user.DataForEx.getSelSubsTeacher().endsWith(amn.user.DataForEx.Teachers.get(i).tid))&&
				(temp.StudHours<=amn.user.DataForEx.Teachers.get(i).FreeHours))
			{
								
					dataTeachers.add(amn.user.DataForEx.Teachers.get(i).FirstName+" "+amn.user.DataForEx.Teachers.get(i).LastName+" "+amn.user.DataForEx.Teachers.get(i).tid);
							
			}
			
			}
		
	
		if(cBRplcTeacher==null)
		{
			cBRplcTeacher = new JComboBox(dataTeachers);
			cBRplcTeacher.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					selRplcTeacher=cBRplcTeacher.getSelectedItem().toString();
			 		System.out.println(selRplcTeacher);
					
					
				}
			});
			cBRplcTeacher.setBounds(242, 248, 178, 20);

			
		}
		return cBRplcTeacher;
	}
	
	private JButton getbtnRequestAuthorization(final AdminMainMenu amn)
	{
		if(btnRequestAuthorization==null)
		{
			 btnRequestAuthorization = new JButton("Request authorization");
			 btnRequestAuthorization.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//TODO WHat happends when you press request authorization
					amn.user.DataForEx.setSelReplaceTeacher(selRplcTeacher);
					String[]tmp=user.DataForEx.getSelClass().split(" ");
					String ClassId=tmp[tmp.length-1];
					tmp=user.DataForEx.getSelSubsTeacher().split(" ");
					String Tid=tmp[tmp.length-1];
					tmp=user.DataForEx.getSelCourse().split(" ");
					String Cid=tmp[tmp.length-1];
					tmp=user.DataForEx.getSelReplaceTeacher().split(" ");
					String RepTid=tmp[tmp.length-1];
					amn.user.DataForEx.RequestCreated=false;
					amn.myLoginFrame.client.handleMessageFromClientUI("CreateExReqTeacherReplacement "+Tid+" "+ClassId+" "+Cid+" "+RepTid);
					while(!amn.user.DataForEx.RequestCreated);
							
					
				}
			});
			btnRequestAuthorization.setBounds(492, 367, 139, 23);
			
		}
		
		return btnRequestAuthorization;
	}
	
	private void initialize(AdminMainMenu amn)
	{
		setBackground(SystemColor.inactiveCaption);
		setLayout(null);
		
		 lblExceptionalRequest = new JLabel("Exceptional request - Teacher allocation from class");
		lblExceptionalRequest.setBounds(231, 11, 243, 14);
		
		
		 lblSelectSubstituteTeacher = new JLabel("Select substitute teacher:");
		lblSelectSubstituteTeacher.setBounds(50, 251, 124, 14);
		this.add(lblSelectSubstituteTeacher);
		this.add(lblExceptionalRequest);
		
		this.add(getcBRplcTeacher(amn));
		
		
		this.add(getbtnRequestAuthorization(amn));
		
	}
	
	
	
	
	
	public TeacherAllocationToClass(AdminMainMenu amn) {
		super();
		initialize(amn);
		
		
	}

}
